import { Sequelize, DataTypes } from 'sequelize';
import initializeDataBase from '@src/db/db';

const UserModel = async () => {
  const sequelize: Sequelize = await initializeDataBase();
  sequelize.define('user', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    username: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: 'username_unique',
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: 'email_unique',
    },
    role: {
      type: DataTypes.ENUM('user', 'admin', 'moderator'),
      allowNull: false,
      defaultValue: 'user',
    },
    avatar: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    signature: {
      type: DataTypes.STRING,
      allowNull: true,
    }
  });

  await sequelize.sync({ alter: true });
  return sequelize.models.user;
};

export default UserModel;